<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/5/20
 * Time: 14:31
 */

namespace common\services;

use common\models\Area;

class AreaSve extends BaseSve
{
    /**
     * 将城市列表转换为键值对 BY:lihouse
     * @return array
     */
    public function cityListToArr()
    {
        $rs = $this->getList(['area_deep' => 2], 'area_id ASC');
        if ($rs['code'] !== 0) {
            return [];
        }
        $tmpArr = [];
        foreach ($rs['data']['list'] as $item) {
            $tmpArr[$item['area_id']] = $item;
        }
        return $tmpArr;
    }


    /**
     * 生成JS对象字符串
     * @param $list
     * @return string
     */
    public function getJS($list){
        $result = '[';
        if(is_array($list) && count($list) > 0){
            $tempArr = [];
            foreach($list as $child){
                $tempArr[] = '{value:"'.$child['area_id'].'",text:"'.$child['area_name'].'",children:'.$this->getJS($child['children']).'}';
            }
            $result .= join(',', $tempArr);
        }
        $result .= ']';
        return $result;
    }

    /**
     * 获取分级列表
     * @return array|mixed
     */
    public function getAreaByLevel()
    {
        //获取地区信息
        $areaList1 = Area::find()->where(['area_deep'=>1])->orderBy('area_deep, area_sort DESC, area_id')->asArray()->all();
        $areaList2 = Area::find()->where(['area_deep'=>2])->orderBy('area_deep, area_sort DESC, area_id')->asArray()->all();
        $areaList3 = Area::find()->where(['area_deep'=>3])->orderBy('area_deep, area_sort DESC, area_id')->asArray()->all();

        $areaList = $this->sortChildrenArea($areaList2, $areaList3);
        $areaList = $this->sortChildrenArea($areaList1, $areaList);
        return $areaList;
    }

    /**
     * 按级排列
     * @param $parent
     * @param $children
     * @return array
     */
    public function sortChildrenArea($parent, $children)
    {
        $result = [];
        foreach($children as $child){
            if(!isset($child['area_parent_id'])){
            }
            if(!isset($result[$child['area_parent_id']])){
                $result[$child['area_parent_id']] = [];
                $result[$child['area_parent_id']]['area_id'] = $child['area_parent_id'];
                $result[$child['area_parent_id']]['children'] = [];
            }
            $result[$child['area_parent_id']]['children'][] = [
                'area_id'=>$child['area_id'],
                'area_name'=>$child['area_name'],
                'children'=>isset($child['children']) ? $child['children']: [],
            ];
        }
        foreach($parent as $val){
            if(!isset($result[$val['area_id']])){
                $result[$val['area_id']] = [
                    'area_id'=>$val['area_id'],
                    'area_parent_id'=>$val['area_parent_id'],
                    'area_name'=>$val['area_name'],
                    'children'=>[]
                ];
            } else {
                $result[$val['area_id']]['area_name'] = $val['area_name'];
                $result[$val['area_id']]['area_parent_id'] = $val['area_parent_id'];
            }
        }
        return array_values($result);
    }
}